package com.junit;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hcfly.entity.Fruit;
import com.hcfly.utils.ESClient;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;


public class FruitDemo {

    private static final ObjectMapper mapper = new ObjectMapper();
    private static final String index = "fruit";
    private static final String type = "species";
    private static final RestHighLevelClient client = ESClient.getClient();
    @Test
    public void createFruitIndex() throws IOException {
        Settings.Builder  settings = Settings.builder()
                .put("number_of_shards", 3)
                .put("number_of_replicas", 1);
        /**
         * public class Fruit {
         *     @JsonIgnore
         *     private Integer id;
         *     private String name;
         *     private Interger size;
         *     @JsonFormat(pattern="yy-MM-dd")
         *     private Date mature;
         *     private String disc;
         *     private String Address;
         * }
         */
        XContentBuilder mapping = JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("name")
                .field("type", "keyword")
                .endObject()
                .startObject("size")
                .field("type", "integer")
                .endObject()
                .startObject("mature")
                .field("type", "date")
                .field("format", "yyyy-MM-dd")
                .endObject()
                .startObject("disc")
                .field("type", "text")
                .field("analyzer", "ik_max_word")
                .endObject()
                .startObject("address")
                .field("type", "text")
                .endObject()

                .endObject()
                .endObject();
        // 3,将setting和mapping封装成一个Request对象
        CreateIndexRequest request = new CreateIndexRequest(index)
                .settings(settings)
                .mapping(type, mapping);

        // 4, 通过client对象去连接ES并执行创建语句
        CreateIndexResponse resp = client.indices().create(request, RequestOptions.DEFAULT);
        // 5, 输出
        System.out.println("Resp: " + resp.toString());
    }

    @Test
    public void BulkOpeartor2() throws IOException, InterruptedException {
        String[] names = new String[]{
                "柚子", "梨","奇异果", "红毛丹","番石榴","菠萝蜜","火龙果","哈密瓜","菠萝", "荔枝", "西瓜", "芒果"
        };
        String des = "鲜红透亮白里透红紫中带绿晶莹透亮又大又圆又香又甜果香四溢酸甜可口清凉爽口扁圆形月牙形甜津津酸甜可口" +
                "清甜爽口甘甜无比又酸又甜清香诱人汁多味甜瓜绿瓤红颗粒饱满香甜可口黄澄澄红通通青里透红红艳" +
                "艳白里透红晶莹洁白的果肉乳白色的果肉新鲜红嫩的果肉黑而发亮的核乳白色的硬核米粒大的核,蜜桃成熟了，" +
                "它努着红扑扑的嘴巴，再配上一身小绒毛，那么好看那么可爱，像害羞的姑娘一样，低垂着头，" +
                "微红着脸那圆圆的橘子像千万盏小灯笼，金灿灿，红彤彤，在墨绿的树叶里灼灼闪光。十几瓣月牙形的桔瓣儿紧紧地团在一起" +
                "，就象一个嫩黄色的小南瓜，惹人喜爱把橘子剥开，一片片月牙似的橘瓣，聚在一起活像盏小灯笼" +
                "它努着红扑扑的嘴巴，再配上一身小绒毛，那么好看那么可爱，像害羞的姑娘一样，低垂着头灿，红彤彤，在墨绿的树叶里灼灼闪";

        String[] addresses = new String[]{
                "北京", "上海", "天津", "重庆", "天水", "石河子","七里河","南宁"
        };
        Fruit fruit;
        String json;
        BulkRequest bulkRequest = new BulkRequest();
        for(int i=0; i < names.length; i++){
            Thread.sleep(100);
            fruit = new Fruit(i+1, names[i], (i*i)%11, new Date(), des.substring(i*20, (i+1)*24),
                    addresses[(i)% addresses.length]);
            json = mapper.writeValueAsString(fruit);
            bulkRequest.add(new IndexRequest(index, type, fruit.getId().toString()).source(json, XContentType.JSON));
        }
        BulkResponse res = client.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

    @Test
    public void BulkOpeartor() throws IOException {
        /**
         * 批量操作
         */

        Fruit fruit1 = new Fruit(1, "香蕉", 11, new Date(), "粗又长", "南宁");
        Fruit fruit2 = new Fruit(2, "苹果", 11, new Date(), "粗又长", "甘肃");
        Fruit fruit3 = new Fruit(3, "梨", 11, new Date(), "粗又长", "南宁");
        Fruit fruit4 = new Fruit(4, "火龙果", 11, new Date(), "大又圆", "南宁");

        String json1 = mapper.writeValueAsString(fruit1);
        String json2 = mapper.writeValueAsString(fruit2);
        String json3 = mapper.writeValueAsString(fruit3);
        String json4 = mapper.writeValueAsString(fruit4);

        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest(index, type, fruit1.getId().toString()).source(json1, XContentType.JSON));
        request.add(new IndexRequest(index, type, fruit2.getId().toString()).source(json2, XContentType.JSON));
        request.add(new IndexRequest(index, type, fruit3.getId().toString()).source(json3, XContentType.JSON));
        request.add(new IndexRequest(index, type, fruit4.getId().toString()).source(json4, XContentType.JSON));

        BulkResponse res = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(res.toString());
    }



}










